package demo.dao;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import demo.model.User;

@Component("daoImpl")
public class TestDaoImpl implements TestDao {
	@Resource
	@Qualifier("sessionFactory")
	SessionFactory sessionFactory;
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	@Transactional(readOnly=true)
	public User getUser(int id) {
		Session session =sessionFactory.getCurrentSession();
		User tmp_user=(User) session.get(User.class, 1);
		System.out.println(tmp_user);
		return null;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void saveUser(User user) {
		Session session =sessionFactory.getCurrentSession();
		session.save(user);
	}

}
